/****************************************************************************
  From Qt Examples
****************************************************************************/

#include "fortuneserver.h"
#include "fortunethread.h"

#include <stdlib.h>

FortuneServer::FortuneServer(QObject *parent)
    : QTcpServer(parent)
{
    fortunes << tr("You've been leading a dog's life. Stay off the furniture.")
             << tr("You've got to think about tomorrow.")
             << tr("You will be surprised by a loud noise.")
             << tr("You are so lucky to be taking ECE 3574!")
             << tr("You are a big fan of inheritance, especially for Qt classes.")
             << tr("Boise State got what it deserved!")
             << tr("Your fate is to go to the NIT forever!")
             << tr("Windows phones are, well, Windows phones.")
             << tr("Signals that call undeclared slots cause many problems.")
             << tr("The owner of a Qt Object handles its events with its event loop.")
             << tr("I would rather be programming in Haskell.")
             << tr("I cannot wait until ECE 4534!")
             << tr("You will feel hungry again in another hour.")
             << tr("You might have mail.")
             << tr("You cannot kill time without injuring eternity.")
             << tr("Computers are not intelligent. They only think they are.");
}

void FortuneServer::incomingConnection(int socketDescriptor)
{
    QString fortune = fortunes.at(qrand() % fortunes.size());
    FortuneThread *thread = new FortuneThread(socketDescriptor, fortune, this);
    connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
    thread->start();
}

